package com.anbinh.doctor.fragment.account;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.anbinh.doctor.DetailtActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentAddHiephoi extends SherlockFragment {
	private static FragmentAddHiephoi fragment = null;
	private View mView;
	private CircleProgressBar loading;
	private EditText edHiephoi;
	private EditText edNoilamviec;

	public static FragmentAddHiephoi newInstance() {
		if (fragment == null) {
			fragment = new FragmentAddHiephoi();
		}
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
			com.actionbarsherlock.view.MenuInflater inflater) {
		menu.add(0, R.id.dia_diem_kham, Menu.NONE, "Hoàn thành")
				.setIcon(R.drawable.ic_done)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		addHiephoi();
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		super.onResume();
		((DetailtActivity) getActivity()).setActionBarTitle(getResources()
				.getString(R.string.them_hiep_hoi));

	}
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_them_hiephoi, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loading = (CircleProgressBar) mView.findViewById(R.id.loadding);
		edHiephoi = (EditText) mView.findViewById(R.id.hiep_hoi);

	}

	public void addHiephoi() {
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		String mChucvu = edHiephoi.getText().toString().trim();
		if (mChucvu.isEmpty()) {
			Utils.showMessage(getActivity(),
					getResources().getString(R.string.vui_long_nhap_hiep_hoi));
			return;
		}

		loading.setVisibility(View.VISIBLE);

		ServerController.getInstance(getActivity()).requestAssociation(token,
				mChucvu, -1, new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						loading.setVisibility(View.INVISIBLE);

						if (data.getResponsecode() >= 0) {
							Intent intent = new Intent();
							intent.putExtra(Utils.KEY, Utils.RESUME_EXTRA_INFO);
							getActivity().setResult(Integer.MAX_VALUE, intent);
							getActivity().finish();
						}

						Utils.showMessage(getActivity(), data.getDescription());
					}

					@Override
					public void onError(int errorCode, String message) {
						loading.setVisibility(View.INVISIBLE);

					}

				});

	}

}
